package com.ruoyi.net.service.impl;

import com.ruoyi.common.constant.ElementConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.domain.*;
import com.ruoyi.net.mapper.BusiElementStatusMapper;
import com.ruoyi.net.mapper.BusiNeighbLabelMapper;
import com.ruoyi.net.service.IBusiNeighbLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * 业务管理-邻接标签Service业务层处理
 * 
 * @author tony
 * @date 2024-12-11
 */
@Service
public class BusiNeighbLabelServiceImpl implements IBusiNeighbLabelService 
{
    @Autowired
    private BusiNeighbLabelMapper busiNeighbLabelMapper;
    @Autowired
    private BusiElementStatusMapper busiElementStatusMapper;
    /**
     * 查询业务管理-邻接标签
     * 
     * @param id 业务管理-邻接标签主键
     * @return 业务管理-邻接标签
     */
    @Override
    public BusiNeighbLabel selectBusiNeighbLabelById(String id)
    {
        return busiNeighbLabelMapper.selectBusiNeighbLabelById(id);
    }

    /**
     * 查询业务管理-邻接标签列表
     * 
     * @param busiNeighbLabel 业务管理-邻接标签
     * @return 业务管理-邻接标签
     */
    @Override
    public List<BusiNeighbLabelListVo> selectBusiNeighbLabelList(BusiNeighbLabel busiNeighbLabel)
    {
        return busiNeighbLabelMapper.selectBusiNeighbLabelList(busiNeighbLabel);
    }

    /**
     * 新增业务管理-邻接标签
     * 
     * @param busiNeighbLabel 业务管理-邻接标签
     * @return 结果
     */
    @Override
    public int insertBusiNeighbLabel(BusiNeighbLabel busiNeighbLabel)
    {
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(busiNeighbLabel.getId())) {
            busiNeighbLabel.setId(uuid);
        }
        busiNeighbLabel.setCreateBy(SecurityUtils.getUsername());
        busiNeighbLabel.setCreateTime(DateUtils.getNowDate());
        busiNeighbLabel.setUpdateBy(SecurityUtils.getUsername());
        busiNeighbLabel.setUpdateTime(DateUtils.getNowDate());
        return busiNeighbLabelMapper.insertBusiNeighbLabel(busiNeighbLabel);
    }

    /**
     * 修改业务管理-邻接标签
     * 
     * @param busiNeighbLabel 业务管理-邻接标签
     * @return 结果
     */
    @Override
    public int updateBusiNeighbLabel(BusiNeighbLabel busiNeighbLabel)
    {
        busiNeighbLabel.setUpdateBy(SecurityUtils.getUsername());
        busiNeighbLabel.setUpdateTime(DateUtils.getNowDate());
        return busiNeighbLabelMapper.updateBusiNeighbLabel(busiNeighbLabel);
    }

    /**
     * 批量删除业务管理-邻接标签
     * 
     * @param ids 需要删除的业务管理-邻接标签主键
     * @return 结果
     */
    @Override
    public int deleteBusiNeighbLabelByIds(String[] ids)
    {
        return busiNeighbLabelMapper.deleteBusiNeighbLabelByIds(ids);
    }

    /**
     * 删除业务管理-邻接标签信息
     * 
     * @param id 业务管理-邻接标签主键
     * @return 结果
     */
    @Override
    public int deleteBusiNeighbLabelById(String id)
    {
        return busiNeighbLabelMapper.deleteBusiNeighbLabelById(id);
    }

    /**
     * 查询网元管理器-sr配置-邻接标签列表
     * 查询elementId为源或宿网元的数据并做标记
     * @param busiNeighbLabelVo
     * @return
     */
    @Override
    public List<BusiNeighbLabelListVo> selectBusiNeighbLabelEleList(BusiNeighbLabelVo busiNeighbLabelVo) {
        ArrayList<BusiNeighbLabelListVo> busiNeighbLabelListVoFinal = new ArrayList<>();
        BusiNeighbLabel busiNeighbLabel = new BusiNeighbLabel();
        busiNeighbLabel.setStartElement(busiNeighbLabelVo.getElementId());
        busiNeighbLabel.setStartPortId(busiNeighbLabelVo.getStartPortId());
        List<BusiNeighbLabelListVo> busiNeighbLabelListVosStart = busiNeighbLabelMapper.selectBusiNeighbLabelList(busiNeighbLabel);
        for (BusiNeighbLabelListVo temp:busiNeighbLabelListVosStart){
            temp.setResultType("start");
        }
        busiNeighbLabelListVoFinal.addAll(busiNeighbLabelListVosStart);
        busiNeighbLabel.setStartElement(null);
        busiNeighbLabel.setEndElement(busiNeighbLabelVo.getElementId());
        List<BusiNeighbLabelListVo> busiNeighbLabelListVosEnd = busiNeighbLabelMapper.selectBusiNeighbLabelList(busiNeighbLabel);
        for (BusiNeighbLabelListVo temp:busiNeighbLabelListVosEnd){
            temp.setResultType("end");
        }
        busiNeighbLabelListVoFinal.addAll(busiNeighbLabelListVosEnd);
        for (BusiNeighbLabelListVo resultVo:busiNeighbLabelListVoFinal){
            //查询激活状态
            BusiElementStatus busiElementStatus = new BusiElementStatus();
            busiElementStatus.setBusiId(resultVo.getId());
            List<BusiElementStatus> statusList = busiElementStatusMapper.selectBusiElementStatusList(busiElementStatus);
            if (!CollectionUtils.isEmpty(statusList)){
                Optional<BusiElementStatus> max = statusList.stream().max(Comparator.comparing(BusiElementStatus::getStatus));
                Optional<BusiElementStatus> min = statusList.stream().min(Comparator.comparing(BusiElementStatus::getStatus));
                //业务设备状态一致 且都为0 未激活，如都为1 已激活   不一致部分激活
                if (max.get().getStatus().equals(min.get().getStatus())){
                    if ("0".equals(max.get().getStatus())){
                        resultVo.setStatus("0");
                    }else {
                        resultVo.setStatus("1");
                    }
                }else {
                    resultVo.setStatus("2");
                }
            }
        }
        return busiNeighbLabelListVoFinal;
    }

    /**
     * 通过网元和端口确定一条邻接标签
     * @param elementId
     * @param portId
     * @return
     */
    @Override
    public BusiNeighbLabelListVo findNeibLabelXmlByParams(String elementId, String portId) {
        BusiNeighbLabelListVo busiNeighbLabelListVo = new BusiNeighbLabelListVo();
        BusiNeighbLabel startNeighParam = new BusiNeighbLabel();
        startNeighParam.setStartElement(elementId);
        startNeighParam.setStartPortId(portId);
        List<BusiNeighbLabelListVo> startNeighs = busiNeighbLabelMapper.selectBusiNeighbLabelList(startNeighParam);

        BusiNeighbLabel endNeighParam = new BusiNeighbLabel();
        endNeighParam.setEndElement(elementId);
        endNeighParam.setEndPortId(portId);

        List<BusiNeighbLabelListVo> endNeighs = busiNeighbLabelMapper.selectBusiNeighbLabelList(endNeighParam);
        if (!CollectionUtils.isEmpty(startNeighs)){
            busiNeighbLabelListVo = startNeighs.get(0);
        }
        if (!CollectionUtils.isEmpty(endNeighs)){
            busiNeighbLabelListVo = endNeighs.get(0);
        }
        return busiNeighbLabelListVo;
    }

    @Override
    public BusiNeighbLabelListVo selectBusiNeighbLabelVoById(String id) {
        return busiNeighbLabelMapper.selectBusiNeighbLabelVoById(id);
    }
}
